package com.example.recuitactivity.viewTables;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.example.recuitactivity.helper.DbOpenHelper;
import com.example.recuitactivity.R;

import java.util.List;

public class  ViewAdminsTableActivity extends AppCompatActivity {
    private TableLayout table;
    private Button btn_next,btn_last;
    private DbOpenHelper dbOpenHelper;
    private List<List<String>> list;
    private int current_count=0,currentItemStart=0,nextItemStart=0;
    private int itemInPage[]=new int[20],pageCount=0,pageIndex=0;
    private boolean isFilled=false;//用于标记当前页是否被填满

    private View.OnClickListener listener=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.btn_admin_next:
                    if(isFilled){
                        pageCount=Math.max(pageCount,pageIndex+1);
                        itemInPage[pageIndex++]=current_count;
                        deleteRowItem();
                        initTable();
                    }
                    break;
                case R.id.btn_admin_last:
                    if(pageIndex>0){
                        nextItemStart=currentItemStart-itemInPage[--pageIndex];
                        deleteRowItem();
                        initTable();
                    }
                    break;
            }
        }
    };
    public void initUI(){
        dbOpenHelper=new DbOpenHelper(ViewAdminsTableActivity.this);
        list=dbOpenHelper.getAllAdmins();

        btn_next=(Button) findViewById(R.id.btn_admin_next);
        btn_last=(Button) findViewById(R.id.btn_admin_last) ;
        table=(TableLayout) findViewById(R.id.tl_admins);
        table.setStretchAllColumns(true);

        btn_next.setOnClickListener(listener);
        btn_last.setOnClickListener(listener);
    }

    public  void initTable(){
        int w= View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        int h= View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);

        currentItemStart=nextItemStart;
        for(int i=nextItemStart;i<list.size();i++){
            TableRow tableRow=new TableRow(ViewAdminsTableActivity.this);
            for(String str:list.get(i)){
                TextView textView=new TextView(ViewAdminsTableActivity.this);
                textView.setBackgroundResource(R.drawable.tv_shape);
                textView.setGravity(Gravity.CENTER);
                textView.setTextSize(20);
                textView.setText(str);
                textView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.MATCH_PARENT));
                tableRow.addView(textView);
            }
            table.addView(tableRow,new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
            table.measure(w,h);
            current_count++;//当前页row计数
            if(table.getMeasuredHeight()>getWindowManager().getDefaultDisplay().getHeight()*0.75){
                isFilled=true;//标记为当前页被填满
                nextItemStart=i+1;//下一页开始项
                break;
            }
            isFilled=false;//当前页未被填满
        }

    }


    public void deleteRowItem(){
        for(int i=0;i<current_count;i++){
            table.removeViewAt(table.getChildCount()-1);
        }
        current_count=0;//当前页count清零
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_admins_table);
        ActionBar actionBar=getSupportActionBar();
        actionBar.setTitle("admins表");
        actionBar.setDisplayHomeAsUpEnabled(true);
        initUI();
        initTable();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if(item.getItemId()==android.R.id.home){
            finish();
            return true;
        }
        return false;
    }
}